package classes.multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author : DuJiabao
 * @Date : 2021/2/9 16:15
 * @Project : java_learning
 * @File : null.java
 * @Desc :
 */
public class ExceptionHandlerTest {
  public static void main(String[] args) {
    ExecutorService exec = Executors.newCachedThreadPool(new MyThreadFactory());
    exec.execute(new Runnable() {
      @Override
      public void run() {
        throw new RuntimeException();
      }
    });
  }
}

class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
  @Override
  public void uncaughtException(Thread t, Throwable e) {
    System.out.println(t.getName() + " caught " + e);
  }
}

class MyThreadFactory implements ThreadFactory {
  @Override
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r);
    t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());   // 设置一个异常处理器
    return t;
  }
}


